package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.text.TextUtils
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemSignDetailsCalBinding
import com.starsea.book.ui.newpage.entity.Today
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.widget.recycler.ItemTouchCallback
import com.starsea.book.utils.visible

class SignDetailsCalAdapter(
    context: Context,
    var today: Today,
    var onChanged: ((value: Today) -> Unit)? = null
) :
    RecyclerAdapter<Today, ItemSignDetailsCalBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemSignDetailsCalBinding {
        return ItemSignDetailsCalBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemSignDetailsCalBinding) {
        binding.rtvSign.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it)
            }
        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemSignDetailsCalBinding,
        item: Today,
        payloads: MutableList<Any>
    ) {
        binding.run {
//            tvTitle.text = item.time

            if (TextUtils.isEmpty(item.date)) {
                rtvDay.text = ""
                rtvSignCoin.visible(false)
                ivIsSign.visible(false)
                rtvSign.visible(false)
            } else {
                rtvDay.text = item.date!!.split("-").last()

                if (TextUtils.isEmpty(item.recordId)) {
                    if (item.time == today.time) {
                        rtvDay.isSelected = true
                        rtvSignCoin.visible()
                        rtvSignCoin.text = "+${(item.amount ?: "0").toDouble().finialDouble()}"
                    } else {
                        //需要补签
                        if (item.time!!.toInt() < today.time!!.toInt()) {
                            rtvSignCoin.visible(false)
                            ivIsSign.visible(false)
                            rtvSign.visible(true)
                            rtvDay.isSelected = false
                            //还未到签到
                        } else {
                            rtvSignCoin.visible(false)
                            ivIsSign.visible(false)
                            rtvSign.visible(false)
                            rtvDay.isSelected = false
                        }
                    }
                    //已签到
                } else {

                    if (item.time == today.time) {
                        rtvSignCoin.text = "+${(item.amount ?: "0").toDouble().finialDouble()}"
                        ivIsSign.visible(false)
                        rtvSign.visible(false)
                        rtvSignCoin.visible()
                        rtvDay.isSelected = false
                    } else {
                        ivIsSign.visible()
                        rtvSign.visible(false)
                        rtvSignCoin.visible(false)
                        rtvDay.isSelected = true
                    }

                }
            }


        }
    }


}